<template>
    <!-- 卖车列表 -->

    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="品牌" prop="brand">
                <el-input v-model="queryParams.brand" placeholder="请输入" clearable></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择创建时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>


            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                </el-col>
            </el-row>


        </el-form>
        <el-table v-loading="loading" :data="tagList">
            <el-table-column label="车辆名称" align="center" prop="brand" />
            <el-table-column label="颜色" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.colour }}</div>
                </template>
            </el-table-column>
            <el-table-column label="价格（万元）" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.guidePrice }}</div>
                </template>
            </el-table-column>
            <el-table-column label="联系方式" align="center" prop="vehicleType">
                <template slot-scope="{row}">
                    <div>{{ row.phone }}</div>

                </template>
            </el-table-column>
            <el-table-column label="任务状态" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.statusName }}</div>
                </template>
            </el-table-column>
            <el-table-column label="寻车有效期" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.lockForTime }}</div>
                </template>
            </el-table-column>



            <el-table-column label="发布时间" align="center">
                <template slot-scope="{row}">
                    <div>{{ row.createTime }}</div>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                    <el-button size="mini" type="text" @click="edit(row)">{{
                        row.status == 1 ? '下架' : '上架' }}</el-button>
                    <el-button size="mini" type="text" @click="views(row)">查看日志</el-button>

                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <el-dialog width="700px" title="新增" :visible.sync="innerVisible" append-to-body>
            <el-form :model="form" ref="addForm" label-width="100px" :rules="rules">
                <el-form-item label="品牌" prop="brand">
                    <el-input style="width: 220px" v-model.trim="form.brand" clearable placeholder="品牌"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="phone">
                    <el-input style="width: 220px" v-model.trim="form.phone" clearable placeholder="手机号"></el-input>
                </el-form-item>

                <el-form-item label="车辆类型" prop="vehicleName">
                    <el-select v-model="form.vehicleName" @change="selectItem" style="width: 220px" clearable>
                        <el-option v-for="(item, index) in options" :key="index" :value="item"
                            :label="item.name"></el-option>

                    </el-select>
                </el-form-item>


                <el-form-item label="颜色" prop="colour">
                    <el-input style="width: 220px" v-model.trim="form.colour" clearable placeholder="颜色"></el-input>
                </el-form-item>


                <el-form-item label="指导价" prop="guidePrice">
                    <el-input style="width: 220px" v-model.trim="form.guidePrice" clearable placeholder="指导价">
                        <template slot="append">万元</template>
                    </el-input>
                </el-form-item>


                <el-form-item label="车源所在地">
                    <provice-select :defaultCity="defaultCity" @changeValue="addStartChangeValue"
                        :inputWidth="150"></provice-select>
                </el-form-item>

                <el-form-item label="寻车有效期" prop="lockForTime">
                    <el-date-picker v-model="form.lockForTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>


                <el-form-item label="备注">
                    <el-input style="width: 220px" type="textarea" v-model.trim="form.remark" clearable
                        placeholder="备注"></el-input>
                </el-form-item>


                <el-form-item label="车辆照片" prop="images1">
                    <image-upload v-model="form.images1" :limit="1" />
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确定</el-button>
                <el-button @click="innerVisible = false">取 消</el-button>
            </div>
        </el-dialog>

        <child-component ref="logRef"></child-component>

    </div>
</template>

<script>
import Enmu from "@/utils/enmu";
import { findCarList, getQuoteList, getLogList } from "@/api/crm/transport";

import { getAdminSellCarPage, below, up, saveAdminIssue } from "@/api/crm/car";

import { forrmatterEnmu } from "@/utils/index"
import { mapGetters } from "vuex"
import ProviceSelect from "@/components/ProviceSelect/index.vue"
import { timestampToDate } from '@/utils/index'
import ChildComponent from "./components/log.vue"

export default {
    name: "Tag",
    data() {
        return {
            baseUrl: process.env.VUE_APP_BASE_API,
            provinces: [],
            cityslist: [],
            queryCitys: [],
            queryRegions: [],
            queryCitysAim: [],
            queryRegionsAim: [],
            defaultAimCity: {},
            defaultCity: {},
            Enmu,
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 标签管理表格数据
            tagList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20
            },
            // 报价列表
            quotationList: [],
            baseInfo: {},
            // 联系列表
            linkList: [],
            quotationPageInfo: {
                total: 0,
                pageSize: 10,
                pageNum: 1
            },
            linkPageInfo: {
                total: 0,
                pageSize: 10,
                pageNum: 1
            },
            innerVisible: false,
            form: {
                brand: '',  //品牌
                vehicleType: '', // 车辆类型 1新车 2二手车 3新能源
                vehicleName: '',
                status: '',  //1上架 2下架
                phone: '',  //手机号
                colour: '',//颜色
                vehicleModel: '', //车型
                guidePrice: '', //指导价
                province: '', //上牌地址 省code
                city: '', //上牌地址 市code
                vehicleLocation: '', //车源所在地
                lockForTime: '', //寻车有效期
                images1: '',
                images: '',
                issueAddress: '1', //1后台发布卖车 2小程序用户发布买车
                remark: '', //备注
            },
            options: [
                {
                    name: '新车',
                    value: '1',
                },
                {
                    name: '二手车',
                    value: '2',
                },
                {
                    name: '新能源',
                    value: '3',
                },
            ],
            rules: {
                brand: [
                    { required: true, message: '请输入品牌', trigger: 'blur' },
                ],
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                ],
                vehicleName: [
                    { required: true, message: '请选择车辆类型', trigger: 'blur' },
                ],
                colour: [
                    { required: true, message: '请输入颜色', trigger: 'blur' },
                ],
                guidePrice: [
                    { required: true, message: '请输入指导价', trigger: 'blur' },
                ],
                images1: [
                    { required: true, message: '请上传照片', trigger: 'blur' },
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    filters: {
        forrmatterTime(time) {
            if (!time || time == '') return "";
            return timestampToDate(time, 'ymdhm');
        }
    },
    computed: {
        ...mapGetters(['allCityList'])
    },
    components: { ChildComponent, ProviceSelect },
    methods: {
        handleAdd() {
            this.innerVisible = true
            this.form = {
                brand: '',  //品牌
                vehicleType: '', // 车辆类型 1新车 2二手车 3新能源
                vehicleName: '',
                status: '',  //1上架 2下架
                phone: '',  //手机号
                images1: '',
                colour: '',//颜色
                vehicleModel: '', //车型
                guidePrice: '', //指导价
                province: '', //上牌地址 省code
                city: '', //上牌地址 市code
                vehicleLocation: '', //车源所在地
                lockForTime: '', //寻车有效期
                issueAddress: '1', //1后台发布卖车 2小程序用户发布买车
                remark: '', //备注
            }

            this.$nextTick(() => {
                this.$refs.addForm.resetFields();
            })
        },
        addStartChangeValue(values) {
            this.form.province = values[0];
            this.form.city = values[1];
        },
        selectItem(item) {
            this.form.vehicleType = item.value
            this.form.vehicleName = item.name
        },

        views(row) {
            this.$refs.logRef.show(row.id)
        },

        submitForm() {
            this.form.images = this.baseUrl + this.form.images1
            console.log(this.form, '123333');

            this.$refs["addForm"].validate(valid => {
                if (valid) {
                    saveAdminIssue(this.form).then(response => {
                        if (response.code == 200) {
                            this.$modal.msgSuccess("新增成功");

                            this.getList();
                        }
                        this.innerVisible = false
                    });
                }
            });
        },
        /** 查询标签管理列表 */
        getList() {
            this.loading = true;
            getAdminSellCarPage(this.queryParams).then(response => {
                this.tagList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },

        edit(row) {
            // 1 下架  2 上架
            let obj = {
                id: row.id,
                status: row.status
            }
            if (row.status == 1) {
                below(obj).then(response => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess("操作成功");
                        this.getList()

                    }
                });
            } else {
                up(obj).then(response => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess("操作成功");
                        this.getList()

                    }
                });

            }
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        }
    }
};
</script>
<style lang="scss" scoped>
.info-content {
    font-size: 14px;

    .base-info {
        display: flex;
        flex-wrap: wrap;
        width: 800px;
    }

    .info-item {
        width: 30%;
        flex: 0 0 30%;
        margin-bottom: 15px;

        span {
            font-weight: 600;
        }
    }

    .info-item-row {
        width: 50%;
        flex: 0 0 50%;
        margin-bottom: 15px;

        span {
            font-weight: 600;
        }
    }
}

.other-info {
    display: flex;
    margin-top: 30px;

    .info-table {
        margin-left: 30px;

        &:first-of-type {
            margin-left: 0;
        }
    }
}

.info-title {
    font-size: 18px;
    margin-bottom: 5px;

    &::before {
        content: "";
        display: inline-block;
        width: 3px;
        height: 15px;
        background: #1777FF;
        border-radius: 3px;
        margin-right: 8px;
    }
}
</style>